/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月25日         cassie
 */

package com.coocaa.launcher.pattern.normal.statusbar;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarHostView;
import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarHostViewController;
import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPluginView;
import com.coocaa.launcher.statusplugins.bluetooth.StatusPluginBluetooth;
import com.coocaa.launcher.statusplugins.calendar.StatusPluginCalendar;
import com.coocaa.launcher.statusplugins.externalstorege.StatusPluginExternalStorage;
import com.coocaa.launcher.statusplugins.net.StatusPluginNet;
import com.coocaa.launcher.statusplugins.tips.StatusPluginTips;
import com.coocaa.launcher.statusplugins.weather.StatusPluginWeather;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

public class StatusBar extends StatusBarHostView {
    private class TopLayout extends FrameLayout {
        private LinearLayout itemLayout = null;

        public TopLayout(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            itemLayout = new LinearLayout(context);
            itemLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.TOP);
            params.topMargin = SkyConstants.Div(15);
            super.addView(itemLayout, params);
        }

        @Override
        public final void addView(View child) {
            // TODO Auto-generated method stub
            itemLayout.addView(child);
        }

        public final void addView(View child, int index, LinearLayout.LayoutParams params) {
            // TODO Auto-generated method stub
            params.leftMargin = SkyConstants.Div(20);
            itemLayout.addView(child, index, params);
        }

        public void addView(View child, LinearLayout.LayoutParams params) {
            // TODO Auto-generated method stub
            params.leftMargin = SkyConstants.Div(20);
            itemLayout.addView(child, params);
        }
    }

    private TopLayout leftLayout = null;
    private TopLayout centerLayout = null;
    private TopLayout rightLayout = null;

    public static StatusBar instance = null;

    public StatusBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        leftLayout = new TopLayout(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT, Gravity.LEFT | Gravity.TOP);
        params.leftMargin = SkyConstants.Div(45);
        addView(leftLayout, params);
        // leftLayout.setLayoutParams(params);

        centerLayout = new TopLayout(context);
        params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,
                Gravity.CENTER_HORIZONTAL);
        addView(centerLayout, params);
        // centerLayout.setLayoutParams(params);

        rightLayout = new TopLayout(context);
        params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,
                Gravity.RIGHT);
        params.rightMargin = SkyConstants.Div(40);
        addView(rightLayout, params);
        // rightLayout.setLayoutParams(params);

        SkinManager.getInstance().setBackgroundResource(this, R.drawable.statusbar_bg);
        params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        setLayoutParams(params);
    }

    @Override
    public void config() {
        // TODO Auto-generated method stub
        StatusBarHostViewController.getInstance().add(StatusPluginBluetooth.instance);
        StatusBarHostViewController.getInstance().add(StatusPluginCalendar.instance);
        StatusBarHostViewController.getInstance().add(StatusPluginNet.instance);
        StatusBarHostViewController.getInstance().add(StatusPluginWeather.instance);
        StatusBarHostViewController.getInstance().add(StatusPluginExternalStorage.instance);
        StatusBarHostViewController.getInstance().add(StatusPluginTips.instance);
    }

    @Override
    public void clearConfig() {
        // TODO Auto-generated method stub
        StatusBarHostViewController.getInstance().remove(StatusPluginBluetooth.instance);
        StatusBarHostViewController.getInstance().remove(StatusPluginCalendar.instance);
        StatusBarHostViewController.getInstance().remove(StatusPluginNet.instance);
        StatusBarHostViewController.getInstance().remove(StatusPluginWeather.instance);
        StatusBarHostViewController.getInstance().remove(StatusPluginExternalStorage.instance);
        StatusBarHostViewController.getInstance().remove(StatusPluginTips.instance);
    }

    private Animation inAnimation, outAnimation = null;
    private int animDuration = 400;

    @Override
    protected Animation getShownAnimation() {
        // TODO Auto-generated method stub
        if (inAnimation == null) {
            // inAnimation = AnimationUtils.loadAnimation(mContext, R.anim.in_updown);
            inAnimation = new TranslateAnimation(0, 0, -100, 0);
            inAnimation.setDuration(animDuration);
            inAnimation.setFillAfter(true);
        }
        return inAnimation;
    }

    @Override
    protected Animation getHidenAnimation() {
        // TODO Auto-generated method stub
        if (outAnimation == null) {
            // inAnimation = AnimationUtils.loadAnimation(mContext, R.anim.in_updown);
            outAnimation = new TranslateAnimation(0, 0, 0, -100);
            outAnimation.setDuration(animDuration);
            outAnimation.setFillAfter(true);
        }
        return outAnimation;
    }

    @Override
    public void add(final StatusBarPluginView view) {
        // TODO Auto-generated method stub
        int gravity = view.getConfigGravity();
        if (gravity == Gravity.LEFT) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, Gravity.TOP);
                    if (view.getParent() == null) {
                        leftLayout.addView(view, 0, params);
                    }
                }
            });
        } else if (gravity == Gravity.RIGHT) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, Gravity.TOP);
                    if (StatusPluginCalendar.instance.getName().equals(view.getName())) // 需求中，日历必须在最右边，所以特殊处理
                        rightLayout.addView(view, params);
                    else {
                        if (view.getParent() == null) {
                            rightLayout.addView(view, 0, params);
                            LogUtils.d("", "sunny add view:" + view);
                        }
                    }

                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, Gravity.TOP);
                    if (view.getParent() == null) {
                        centerLayout.addView(view, params);
                    }
                }
            });
        }
    }

    @Override
    public void remove(final StatusBarPluginView view) {
        // TODO Auto-generated method stub

        runOnUiThread(new Runnable() {
            public void run() {
                leftLayout.removeView(view);
                centerLayout.removeView(view);
                rightLayout.removeView(view);
            }
        });
    }

}
